//
//  PicCacheHelper.m
//  WoWeibo
//
//  Created by  on 12-5-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PicCacheHelper.h"

@interface PicCacheHelper (Private)

- (void)checkDirectoryPath;
- (NSDate *)getLastDate;

@end

@implementation PicCacheHelper

- (id)init {

    self = [super init];
    
    if(self != nil) {
    
        [self checkDirectoryPath];
    }
    
    return self;
}

- (BOOL)storeImage:(UIImage *)image withMD5:(NSString *)md5 {
    
    assert(image != nil);
    assert(md5 != nil);
    assert(md5.length > 0);  

    return [self storeData:UIImageJPEGRepresentation(image, 1.0f) withMD5:md5];
}

- (BOOL)storeData:(NSData *)data withMD5:(NSString *)md5 {

    assert(data != nil);
    assert(md5 != nil);
    assert(md5.length > 0);
    
    return [data writeToFile:[_directoryPath stringByAppendingPathComponent:md5] atomically:YES];
}

- (UIImage *)getImage:(NSString *)md5 {

    assert(md5 != nil);
    assert(md5.length > 0);
    
    NSData *data = [self getData:md5];
    if(data == nil) {
        
        return nil;
    }else {
    
        UIImage *image = [UIImage imageWithData:data];
        return image;
    }
    
    return nil;
}

- (NSData *)getData:(NSString *)md5 {

    assert(md5 != nil);
    assert(md5.length > 0);
    
    NSData *data = [NSData dataWithContentsOfFile:[_directoryPath stringByAppendingPathComponent:md5]];
    
    return data;
}

- (void)cleanCache {

    NSDate *now = [NSDate date];
    NSDate *lastModificationDate = [self getLastDate];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];
    NSDateComponents *lastModificationComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:lastModificationDate];
    if(nowComponents.year != lastModificationComponents.year 
       || nowComponents.month != lastModificationComponents.month
       || nowComponents.day != lastModificationComponents.day) {
    
        [[NSFileManager defaultManager] removeItemAtPath:_directoryPath error:nil];
        [self checkDirectoryPath];
    }
    
    [calendar release];
}

#pragma mark -
#pragma mark Private Methods.

- (void)checkDirectoryPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    _directoryPath = [[(NSString *)[paths objectAtIndex:0] stringByAppendingPathComponent:@"PicCache"] retain];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:_directoryPath] == NO) {
        
        [fileManager createDirectoryAtPath:_directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (NSDate *)getLastDate {

    NSDictionary *properties = [[NSFileManager defaultManager] attributesOfItemAtPath:_directoryPath error:nil];
    NSDate *lastModificationDate = [properties objectForKey:NSFileModificationDate];
    
    return lastModificationDate;
}

@end
